﻿<%@ Page Language="VB" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    
    Public strMessage As String
    Dim strQueryOrdering As String = " ORDER BY Projects.ProjectCode "
   
    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim sC1 As String
        Dim sWhere As String = ""
        
        If Session("userLevel") = "" Then
            Session("userLevel") = "3"
        End If
        
        '	Authorization level 3 needed for this function
        If Int32.Parse(Session("userLevel")) < 3 Then
            System.Diagnostics.Debug.Write(Session("userLevel"))
            Response.Redirect("/timereport/menu.asp?msgtype=E&msgno=" & ConfigurationSettings.AppSettings("MSGNO_AUTHORIZATION_FAILED"))
        End If
        
        ' Imposta il SelectCommand in base al contenuto della lista dropdown
        If DL_flattivo.SelectedValue <> "all" Or _
          (Session("DL_flattivo_val1") <> Nothing And Not IsPostBack) Then
            sWhere = " WHERE Projects.Active IN (@DL_flattivo)"
        End If
        
        If DL_manager.SelectedValue <> "all" Or _
          (Session("DL_manager_val1") <> Nothing And Not IsPostBack) Then
            sWhere = IIf(sWhere = "", " WHERE Projects.ClientManager_id = (@DL_manager)", sWhere & " AND Projects.ClientManager_id = (@DL_manager)")
        End If
        
        If DL_clienti.SelectedValue <> "all" Or _
          (Session("DL_clienti_val") <> Nothing And Not IsPostBack) Then
            sWhere = IIf(sWhere = "", " WHERE Projects.CodiceCliente = (@DL_clienti)", sWhere & " AND Projects.CodiceCliente = (@DL_clienti)")
        End If

        If TB_Codice.Text <> Nothing Or _
            (Session("TB_Codice_val") <> Nothing And Not IsPostBack) Then
            sC1 = IIf(sWhere = "", " WHERE ", " AND ")
            sWhere = sWhere & sC1 & "Projects.ProjectCode LIKE '%' + (@TB_Codice) + '%' "
        End If
        
        SqlDataSource1.SelectCommand = "SELECT Projects.Projects_Id, Projects.ProjectCode, Projects.Name AS ProjectName, Projects.ProjectType_Id, Projects.Active, Projects.ClientManager_id, Persons.Name AS ManagerName, ProjectType.Name AS ProjectType, Customers.Nome1, Projects.ImportoRevenue FROM Projects LEFT OUTER JOIN Persons ON Projects.ClientManager_id = Persons.Persons_id LEFT OUTER JOIN ProjectType ON Projects.ProjectType_Id = ProjectType.ProjectType_Id LEFT OUTER JOIN Customers ON Projects.CodiceCliente = Customers.CodiceCliente" & sWhere & strQueryOrdering
                
    End Sub
            
    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Redirect("projects_lookup_form.aspx?Projectcode=" & GridView1.SelectedValue)
    End Sub
    
    Protected Sub DL_manager_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("DL_manager_val1") = IIf(sender.selectedValue <> "all", sender.selectedValue, Nothing)
    End Sub
    
    Protected Sub DL_clienti_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("DL_clienti_val") = IIf(sender.selectedValue <> "all", sender.selectedValue, Nothing)
    End Sub

    Protected Sub DL_flattivo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("DL_flattivo_val1") = IIf(sender.selectedValue <> "all", sender.selectedValue, Nothing)
    End Sub
    
    Protected Sub TB_Codice_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("TB_Codice_val") = sender.text
    End Sub
        
    Protected Sub TB_Codice_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Resetta valore textbox per non perderlo a seguito passaggio a pagina di dettaglio
        If Not IsPostBack And Session("TB_Codice_val") <> Nothing Then
            TB_Codice.Text = Session("TB_Codice_val")
        End If
    End Sub
    
    Protected Sub DL_manager_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Resetta indice di selezione sulle dropdwonlist per non perderlo a seguito passaggio a pagina di dettaglio
        If Not IsPostBack And Session("DL_manager_val1") <> Nothing Then
            DL_manager.SelectedValue = Session("DL_manager_val1")
        End If
    End Sub

    Protected Sub DL_clienti_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Resetta indice di selezione sulle dropdwonlist per non perderlo a seguito passaggio a pagina di dettaglio
        If Not IsPostBack And Session("DL_clienti_val") <> Nothing Then
            DL_clienti.SelectedValue = Session("DL_clienti_val")
        End If
    End Sub
    
    Protected Sub DL_flattivo_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Resetta indice di selezione sulle dropdwonlist per non perderlo a seguito passaggio a pagina di dettaglio
        If Not IsPostBack And Session("DL_flattivo_val1") <> Nothing Then
            DL_flattivo.SelectedValue = Session("DL_flattivo_val1")
        End If
    End Sub
    
    Protected Sub BtnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        
        Utilities.ExportXls("SELECT Projects.*, Persons.Name FROM Projects LEFT OUTER JOIN Persons ON Projects.ClientManager_id = Persons.Persons_id")

    End Sub
    
    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs)

        Dim c = New ValidationClass
        
        '       verifica integrità database        
        If c.CheckExistence("Projects_id", e.Keys(1), "Hours") Then
            e.Cancel = True
            ' Call separate class, passing page reference, to register Client Script:
            Utilities.CreateMessageAlert(Me, "Cancellazione non possibile, cliente utilizzato su tabella ore", "strKey1")
        End If

        If c.CheckExistence("Projects_id", e.Keys(1), "Expenses") Then
            e.Cancel = True
            ' Call separate class, passing page reference, to register Client Script:
            Utilities.CreateMessageAlert(Me, "Cancellazione non possibile, cliente utilizzato su tabella spese", "strKey1")
        End If
        
    End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Lista progetti</title>
    <link href="/timereport/include/commoncss.aspx" rel="stylesheet" type="text/css" />
</head>

<script language="JavaScript" src="/timereport/include/menu/menu_array.js" type="text/javascript"></script>

<script language="JavaScript" src="/timereport/include/menu/mmenu.js" type="text/javascript"></script>

<body>
    <table width="760" border="0">
        <tr>
            <td height="30" class="BarraTop" align="right">
                Utente:
                <%= session("UserName") %>
            </td>
    </table>
    <br />
    <form id="form1" runat="server">

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

    <asp:updatepanel id="pannello1" runat="server">
    <ContentTemplate>
    <table width="760" border="0" class="GridTab">
        </tr>
        <td>
            Attivo:
        </td>
        <td>
            <asp:DropDownList ID="DL_flattivo" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DL_flattivo_SelectedIndexChanged"
                OnPreRender="DL_flattivo_Load" CssClass="TabellaLista">
                <asp:ListItem Value="all">Tutti i valori</asp:ListItem>
                <asp:ListItem Selected="True" Value="1">Attivo</asp:ListItem>
                <asp:ListItem Value="0">Non attivo</asp:ListItem>
            </asp:DropDownList>
        </td>
        <td>
            Manager:
        </td>
        <td>
            <asp:DropDownList ID="DL_manager" runat="server" AutoPostBack="True" AppendDataBoundItems="true"
                DataSourceID="DS_Persone" DataTextField="Name" DataValueField="Persons_id" OnSelectedIndexChanged="DL_manager_SelectedIndexChanged"
                OnDataBound="DL_manager_DataBound" Width="150px" CssClass="TabellaLista">
                <asp:ListItem Text="Tutti i valori" Value="all" />
            </asp:DropDownList>
        </td>
        </tr>
        <tr>
            <td>
                Codice:
            </td>
            <td>
                <asp:TextBox ID="TB_Codice" runat="server" OnTextChanged="TB_Codice_TextChanged"
                    OnLoad="TB_Codice_Load" CssClass="TabellaLista"></asp:TextBox>
                &nbsp;<asp:Button ID="BTFiltra" runat="server" Text="filtra" />
            </td>
            <td>
                Cliente:
            </td>
            <td>
                <asp:DropDownList ID="DL_clienti" runat="server" AutoPostBack="True" AppendDataBoundItems="True"
                    DataSourceID="DS_clienti" DataTextField="Nome1" DataValueField="CodiceCliente"
                    Width="150px" OnSelectedIndexChanged="DL_clienti_SelectedIndexChanged" OnDataBound="DL_clienti_DataBound"
                    CssClass="TabellaLista">
                    <asp:ListItem Text="Tutti i valori" Value="all" />
                </asp:DropDownList>
                <asp:SqlDataSource ID="DS_clienti" runat="server" ConnectionString="<%$ ConnectionStrings:MSSql12155ConnectionString %>"
                    SelectCommand="SELECT DISTINCT Customers.CodiceCliente, Customers.Nome1 FROM Customers INNER JOIN Projects ON Customers.CodiceCliente = Projects.CodiceCliente WHERE (Customers.FlagAttivo = @FlagAttivo) ORDER BY Customers.Nome1">
                    <SelectParameters>
                        <asp:Parameter DefaultValue="true" Name="FlagAttivo" Type="Boolean" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </td>
        </tr>
    </table>
    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
        DataSourceID="SqlDataSource1" CssClass="GridView" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
        AllowPaging="True" PageSize="15" DataKeyNames="projectcode,projects_id" OnRowDeleting="GridView1_RowDeleting"
        GridLines="None">
        <FooterStyle CssClass="GV_footer" />
        <RowStyle Wrap="False" CssClass="GV_row" />
        <Columns>
            <asp:BoundField DataField="ProjectCode" HeaderText="Codice" SortExpression="ProjectCode" />
            <asp:BoundField DataField="ProjectName" HeaderText="Progetto" SortExpression="ProjectName" />
            <asp:BoundField DataField="Nome1" HeaderText="Cliente" SortExpression="Nome1" />
            <asp:BoundField DataField="ManagerName" HeaderText="Manager " SortExpression="ManagerName" />
            <asp:BoundField DataField="ProjectType" HeaderText="ProjectType" SortExpression="ProjectType" />
            <asp:CheckBoxField DataField="Active" HeaderText="Attivo" SortExpression="Active" />
            <asp:BoundField DataField="ImportoRevenue" HeaderText="Revenue" SortExpression="ImportoRevenue" />
            <asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" ButtonType="Image"
                DeleteImageUrl="/timereport/images/icons/16x16/trash.gif" SelectImageUrl="/timereport/images/icons/16x16/modifica.gif" />
        </Columns>
        <PagerStyle CssClass="GV_footer" />
        <HeaderStyle CssClass="GV_header" />
        <AlternatingRowStyle CssClass="GV_row_alt " />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MSSql12155ConnectionString %>"
        DeleteCommand="DELETE FROM [Projects] WHERE [ProjectCode] = @ProjectCode" SelectCommand="SELECT Projects.Projects_Id, Projects.ProjectCode, Projects.Name AS ProjectName, Projects.ProjectType_Id, Projects.Active, Projects.ClientManager_id, Persons.Name AS ManagerName, ProjectType.Name AS ProjectType, Customers.Nome1, Projects.ImportoRevenue FROM Projects LEFT OUTER JOIN Persons ON Projects.ClientManager_id = Persons.Persons_id LEFT OUTER JOIN ProjectType ON Projects.ProjectType_Id = ProjectType.ProjectType_Id LEFT OUTER JOIN Customers ON Projects.CodiceCliente = Customers.CodiceCliente"
        UpdateCommand="UPDATE [Projects] SET [ProjectCode] = @ProjectCode, [Name] = @Name, [ProjectType_Id] = @ProjectType_Id, [Channels_Id] = @Channels_Id, [Company_id] = @Company_id, [Active] = @Active, [Always_available] = @Always_available, [ClientManager_id] = @ClientManager_id, [TipoContratto] = @TipoContratto, [ImportoRevenue] = @ImportoRevenue, [ImportoSpese] = @ImportoSpese, [SpeseForfait] = @SpeseForfait, [ImportoSpeseForfait] = @ImportoSpeseForfait, [RevenueFatturate] = @RevenueFatturate, [SpeseFatturate] = @SpeseFatturate, [Incassato] = @Incassato, [PianoFatturazione] = @PianoFatturazione, [MetodoPagamento] = @MetodoPagamento, [TerminiPagamento] = @TerminiPagamento WHERE [Projects_Id] = @Projects_Id"
        InsertCommand="INSERT INTO [Projects] ([ProjectCode], [Name], [ProjectType_Id], [Channels_Id], [Company_id], [Active], [Always_available], [ClientManager_id], [TipoContratto], [ImportoRevenue], [ImportoSpese], [SpeseForfait], [ImportoSpeseForfait], [RevenueFatturate], [SpeseFatturate], [Incassato], [PianoFatturazione], [MetodoPagamento], [TerminiPagamento]) VALUES (@ProjectCode, @Name, @ProjectType_Id, @Channels_Id, @Company_id, @Active, @Always_available, @ClientManager_id, @TipoContratto, @ImportoRevenue, @ImportoSpese, @SpeseForfait, @ImportoSpeseForfait, @RevenueFatturate, @SpeseFatturate, @Incassato, @PianoFatturazione, @MetodoPagamento, @TerminiPagamento)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DL_flattivo" Name="DL_flattivo" PropertyName="SelectedValue"
                Type="String" />
            <asp:ControlParameter ControlID="DL_manager" Name="DL_manager" PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="DL_clienti" Name="DL_clienti" PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="TB_Codice" Name="TB_Codice" PropertyName="text"
                DefaultValue="%" />
        </SelectParameters>
        <DeleteParameters>
            <asp:Parameter Name="ProjectCode" Type="string" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="ProjectCode" Type="String" />
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="ProjectType_Id" Type="Int32" />
            <asp:Parameter Name="Channels_Id" Type="Int32" />
            <asp:Parameter Name="Company_id" Type="Int32" />
            <asp:Parameter Name="Active" Type="Boolean" />
            <asp:Parameter Name="Always_available" Type="Boolean" />
            <asp:Parameter Name="ClientManager_id" Type="Int32" />
            <asp:Parameter Name="TipoContratto" Type="Int32" />
            <asp:Parameter Name="ImportoRevenue" Type="Decimal" />
            <asp:Parameter Name="ImportoSpese" Type="Decimal" />
            <asp:Parameter Name="SpeseForfait" Type="Boolean" />
            <asp:Parameter Name="ImportoSpeseForfait" Type="Decimal" />
            <asp:Parameter Name="RevenueFatturate" Type="Decimal" />
            <asp:Parameter Name="SpeseFatturate" Type="Decimal" />
            <asp:Parameter Name="Incassato" Type="Decimal" />
            <asp:Parameter Name="PianoFatturazione" Type="String" />
            <asp:Parameter Name="MetodoPagamento" Type="String" />
            <asp:Parameter Name="TerminiPagamento" Type="String" />
            <asp:Parameter Name="Projects_Id" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="ProjectCode" Type="String" />
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="ProjectType_Id" Type="Int32" />
            <asp:Parameter Name="Channels_Id" Type="Int32" />
            <asp:Parameter Name="Company_id" Type="Int32" />
            <asp:Parameter Name="Active" Type="Boolean" />
            <asp:Parameter Name="Always_available" Type="Boolean" />
            <asp:Parameter Name="ClientManager_id" Type="Int32" />
            <asp:Parameter Name="TipoContratto" Type="Int32" />
            <asp:Parameter Name="ImportoRevenue" Type="Decimal" />
            <asp:Parameter Name="ImportoSpese" Type="Decimal" />
            <asp:Parameter Name="SpeseForfait" Type="Boolean" />
            <asp:Parameter Name="ImportoSpeseForfait" Type="Decimal" />
            <asp:Parameter Name="RevenueFatturate" Type="Decimal" />
            <asp:Parameter Name="SpeseFatturate" Type="Decimal" />
            <asp:Parameter Name="Incassato" Type="Decimal" />
            <asp:Parameter Name="PianoFatturazione" Type="String" />
            <asp:Parameter Name="MetodoPagamento" Type="String" />
            <asp:Parameter Name="TerminiPagamento" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="DS_Persone" runat="server" ConnectionString="<%$ ConnectionStrings:MSSql12155ConnectionString %>"
        
        SelectCommand="SELECT DISTINCT Persons.Persons_id, Persons.Name FROM Persons INNER JOIN Projects ON Persons.Persons_id = Projects.ClientManager_id WHERE (Persons.Active = @Active) ORDER BY Persons.Name">
        <SelectParameters>
            <asp:Parameter DefaultValue="true" Name="Active" Type="Boolean" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:Button ID="btn_back" runat="server" Text="Cancella" CssClass="Gridbutton" PostBackUrl="/timereport/menu.asp" />
    <asp:Button ID="btn_crea" runat="server" Text="Crea record" CssClass="Gridbutton"
        PostBackUrl="/timereport/m_gestione/projects_lookup_form.aspx" />
    <asp:Button ID="BtnExport" runat="server" CssClass="Gridbutton" OnClick="BtnExport_Click"
        Text="Export to Excel" />
 
    </ContentTemplate>
    </asp:updatePanel>
 
    </form>
</body>
</html>
